package util;

import vo.StockInfo;
import vo.UserInterest;
import java.io.*;
import java.util.ArrayList;

public class FileHandlerImpl implements FileHandler {

    /**
     * This func gets stock information from the given interfaces path.
     * If interfaces don't exit,or it has a illegal/malformed format, return NULL.
     * The filepath can be a relative path or a absolute path
     *
     * @param filePath
     * @return the Stock information array from the interfaces,or NULL
     */
    @Override
    public StockInfo[] getStockInfoFromFile(String filePath) {
        File file = new File(filePath);
        StockInfo[] all = null;
        if (file.isFile()&&file.exists()) {
            try {
                String s = null;
                ArrayList<StockInfo> list = new ArrayList<StockInfo>();
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),"UTF-8");
                BufferedReader bufferedReader = new BufferedReader(read);
                while ((s=bufferedReader.readLine())!=null) {
                    StockInfo info = new StockInfo(s);
                    list.add(info);
                }
                int size = list.size();
                all = new StockInfo[size];
                list.toArray(all);
                read.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return all;
        }
        else return null;
    }

    /**
     * This func gets user interesting from the given interfaces path.
     * If interfaces don't exit,or it has a illegal/malformed format, return NULL.
     * The filepath can be a relative path or a absolute path
     *
     * @param filePath
     * @return
     */
    @Override
    public UserInterest[] getUserInterestFromFile(String filePath) {
        File file = new File(filePath);
        UserInterest[] all = null;
        if (file.isFile()&&file.exists()) {
            try {
                String s = null;
                ArrayList<UserInterest> list = new ArrayList<UserInterest>();
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(read);
                while ((s=bufferedReader.readLine())!=null) {
                    UserInterest interest = new UserInterest(s);
                    list.add(interest);
                }
                all = new UserInterest[list.size()];
                list.toArray(all);
                read.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return all;
        }
        else return null;
    }

    /**
     * This function need write matrix to files
     *
     * @param matrix the matrix you calculate
     */
    @Override
    public void setCloseMatrix2File(double[][] matrix) {
        //TODO: write your code here
        File file = new File("D:/IDEA/homework_2/src/main/resources/Matrix.txt");
        try {
            FileWriter fw = new FileWriter(file);
            PrintWriter pw = new PrintWriter(fw);
            for (int i=1;i<matrix.length;i++) {
                for (int j=1;j<matrix.length;j++) {
                    pw.print(matrix[i][j]+"\t");
                    pw.flush();
                }
                pw.print("\n");
            }
            pw.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This function need write recommend to files
     *
     * @param recommend the recommend you calculate
     */
    @Override
    public void setRecommend2File(double[][] recommend) {
        //TODO: write your code here
        File file = new File("D:/IDEA/homework_2/src/main/resources/Recommend.txt");
        try {
            FileWriter fw = new FileWriter(file);
            PrintWriter pw = new PrintWriter(fw);
            for (int i=499;i>=0;i--) {
                for (int j=0;j<10;j++) {
                    pw.print(recommend[i][j]+"\t");
                    pw.flush();
                }
                pw.print("\n");
            }
            pw.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
